/**
 * 
 */
package vn.smartware.co.framework.plugin;

import java.io.FileInputStream;
import java.io.Serializable;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import vn.smartware.co.framework.common.BaseConstant;
import vn.smartware.co.java.net.commons.Utils;

/**
 * SystemConfig
 */
public class SystemResources implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -66093895362444196L;

	private static Log log = LogFactory.getLog(SystemResources.class);
	
	private static SystemResources systemResources;
	
	/**
	 * getInstance
	 * @return SystemResources
	 */
	public static SystemResources getInstance(){
		
		if (Utils.isNullEmpty(systemResources)){
			String systemResourcePath = BaseConstant.CLIENT_SYSTEM_RESOURCES_FILE;
			readClientSystemResources(systemResourcePath);
		}
		return systemResources;
	}
	
	/**
	 * getInstance
	 * @param context 
	 * @return SystemResources
	 */
	public static SystemResources getInstance(ServletContext context) {
		
		if (Utils.isNullEmpty(systemResources)){
			String systemResourcePath = context.getRealPath("") + BaseConstant.SERVER_SYSTEM_RESOURCES_FILE;
			readServerSystemResources(systemResourcePath);
		}
		return systemResources;
	}
	
	/**
	 * readServerSystemResources
	 */
    private static void readServerSystemResources(String systemResourcePath) {
		
    	try {
			Properties p = new Properties();
			p.load(new FileInputStream(systemResourcePath));
			
			String langAbbr = p.getProperty("language");
			String languageFile = p.getProperty("language_file");
			String supportLangs = p.getProperty("support_language");
			
			systemResources = new SystemResources();
			systemResources.setConfigFile(p.getProperty("config_file"));
			systemResources.setLanguageFile(langAbbr + "_" + languageFile);
			systemResources.setCurrentLanguage(langAbbr);
			systemResources.setSupportLanguages(supportLangs.split(","));
			
			log.debug("======== Language Configuration ========");
			log.debug("Language = " + langAbbr);
		} 
    	catch (Exception e) {
			log.error(e.getCause());
			e.printStackTrace();
		}
	}
    
    /**
	 * readClientSystemResources
	 */
    private static void readClientSystemResources(String systemResourcePath) {
		
    	try {
    		ClassLoader loader = SystemResources.class.getClassLoader();
			Properties p = new Properties();
			p.load(loader.getResourceAsStream(systemResourcePath));
			
			String langAbbr = p.getProperty("language");
			String resourceFile = p.getProperty("client_resource_file");
			String languageFile = p.getProperty("language_file");
			String supportLangs = p.getProperty("support_language");
			
			systemResources = new SystemResources();
			systemResources.setConfigFile(p.getProperty("config_file"));
			systemResources.setClientResourceFile(langAbbr + "_" + resourceFile);
			systemResources.setLanguageFile(langAbbr + "_" + languageFile);
			systemResources.setSupportLanguages(supportLangs.split(","));
			systemResources.setCurrentLanguage(langAbbr);
			
			log.debug("======== System Resource Files ========");
			log.debug("Config file = " + systemResources.getConfigFile());
			log.debug("Client resource file = " + systemResources.getClientResourceFile());
			log.debug("Support Language = " + systemResources.getSupportLanguages().toString());
			log.debug("Language = " + langAbbr);
			log.debug("Language file = " + systemResources.getLanguageFile());
			
		} catch (Exception e) {
			log.error(e.getCause());
			e.printStackTrace();
		}
	}
    
	private String configFile;
    
    private String clientResourceFile;
    
    private String languageFile;
    
    private String currentLanguage;
    
    private String[] supportLanguages;

	/**
	 * @return the languageFile
	 */
	public String getLanguageFile() {
		return languageFile;
	}

	/**
	 * @param languageFile the languageFile to set
	 */
	public void setLanguageFile(String languageFile) {
		this.languageFile = languageFile;
	}

	/**
	 * @return the clientResourceFile
	 */
	public String getClientResourceFile() {
		return clientResourceFile;
	}

	/**
	 * @param clientResourceFile the clientResourceFile to set
	 */
	public void setClientResourceFile(String clientResourceFile) {
		this.clientResourceFile = clientResourceFile;
	}

	/**
	 * @return the configFile
	 */
	public String getConfigFile() {
		return configFile;
	}

	/**
	 * @param configFile the configFile to set
	 */
	public void setConfigFile(String configFile) {
		this.configFile = configFile;
	}

	/**
	 * @return the supportLanguages
	 */
	public String[] getSupportLanguages() {
		return supportLanguages;
	}

	/**
	 * @param supportLanguages the supportLanguages to set
	 */
	public void setSupportLanguages(String[] supportLanguages) {
		this.supportLanguages = supportLanguages;
	}

	/**
	 * @return the currentLanguage
	 */
	public String getCurrentLanguage() {
		return currentLanguage;
	}

	/**
	 * @param currentLanguage the currentLanguage to set
	 */
	public void setCurrentLanguage(String currentLanguage) {
		this.currentLanguage = currentLanguage;
	}
}
